package leetcode.part3;

import org.junit.Test;

public class S202 {
    public boolean isHappy(int n) {
        int slow = n, fast = n;
        do {
            slow = bitSquareSum(slow);
            // 快指针的迭代速度当然得快点
            fast = bitSquareSum(fast);
            fast = bitSquareSum(fast);
        } while (slow != fast);
        return slow == 1;
    }

    int bitSquareSum(int n) {
        int sum = 0;
        while (n > 0) {
            int bit = n % 10;
            sum += bit * bit;
            n = n / 10;
        }
        return sum;
    }

    @Test
    public void test() {
        System.out.println(isHappy(19));
        System.out.println(isHappy(2));
    }
}
